/**
 * Copyright (C) 2013-2014 EaseMob Technologies. All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.wephone.Student.huanxin.activity;

import java.util.ArrayList;
import java.util.List;

import com.loopj.android.http.RequestParams;
import com.wephone.Student.BaseApplication;
import com.wephone.Student.R;
import com.wephone.Student.bean.GroupMessageResponse;
import com.wephone.Student.bean.data.GroupInvite;
import com.wephone.Student.bean.data.LiuxuequanInfoData;
import com.wephone.Student.huanxin.Constant;
import com.wephone.Student.huanxin.adapter.NewFriendsMsgAdapter;
import com.wephone.Student.huanxin.db.InviteMessgeDao;
import com.wephone.Student.huanxin.domain.InviteMessage;
import com.wephone.Student.network.HttpClient;
import com.wephone.Student.network.MyResponseHandler;
import com.wephone.Student.ui.BaseActivity;
import com.wephone.Student.util.Constants;
import com.wephone.Student.util.PullToRefreshView;
import com.wephone.Student.util.PullToRefreshView.OnFooterRefreshListener;
import com.wephone.Student.util.PullToRefreshView.OnHeaderRefreshListener;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;


/**
 * 申请与通知
 *
 */
public class NewFriendsMsgActivity extends BaseActivity {
	private ListView listView;
	private ListView listView1;
	private ProgressBar progressBar;
	public static NewFriendsMsgActivity s_instance;
	private ViewPager viewPager;
	private List<Button> statusBarList = new ArrayList<Button>();
	private List<View> mSubViews = new ArrayList<View>();
	private NewFriendsMsgAdapter adapter;
	private NewFriendsMsgAdapter adapter1;
	private List<GroupInvite> invites = new ArrayList<GroupInvite>();
	private List<GroupInvite> invites1= new ArrayList<GroupInvite>();
	private int type;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_friends_msg);
		
		s_instance = this;
		
		Bundle bundle = getIntent().getExtras();
		type = bundle.getInt("type");
		
		refresh1();
		
	}
	public void refresh(){
		if(viewPager.getCurrentItem()==0){
			updateInviteMessage();
		}else{
			updateInviteMessage1();
		}
	}
	public void refresh1(){
		viewPager = (ViewPager)findViewById(R.id.viewpager);
		statusBarList.add((Button)findViewById(R.id.menu1));
		statusBarList.add((Button)findViewById(R.id.menu2));
		statusBarList.get(0).setTag(0);
		statusBarList.get(0).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int idx = Integer.parseInt(arg0.getTag().toString());
				viewPager.setCurrentItem(idx, true);
			}
		});
		statusBarList.get(1).setTag(1);
		statusBarList.get(1).setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View arg0) {
				int idx = Integer.parseInt(arg0.getTag().toString());
				viewPager.setCurrentItem(idx, true);
			}
		});
	
		View v1 = getLayoutInflater().inflate(R.layout.include_message_list_view, null);
		listView = (ListView)v1.findViewById(R.id.listview);
		adapter = new NewFriendsMsgAdapter(NewFriendsMsgActivity.this, 1, invites); 
		listView.setAdapter(adapter);
		
		mSubViews.add(v1);
		View v2 = getLayoutInflater().inflate(R.layout.include_message_list_view1, null);
		listView1 = (ListView)v2.findViewById(R.id.listview1);
		adapter1 = new NewFriendsMsgAdapter(NewFriendsMsgActivity.this, 1, invites1); 
		listView1.setAdapter(adapter1);
		
		mSubViews.add(v2);
		
		viewPager.setAdapter(new ViewAdapter());
		viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
			@Override
			public void onPageSelected(int arg0) {
				if(arg0==0){
					((Button)findViewById(R.id.menu1)).setTextColor(getResources().getColor(R.color.orange));
					((Button)findViewById(R.id.menu2)).setTextColor(getResources().getColor(R.color.default_black));
					((ImageView)findViewById(R.id.top_bar_status_image_1)).setBackgroundColor(getResources().getColor(R.color.orange));
					((ImageView)findViewById(R.id.top_bar_status_image_2)).setBackgroundColor(getResources().getColor(R.color.default_white));
					 updateInviteMessage();
				}else if(arg0==1){
					((Button)findViewById(R.id.menu2)).setTextColor(getResources().getColor(R.color.orange));
					((Button)findViewById(R.id.menu1)).setTextColor(getResources().getColor(R.color.default_black));
					((ImageView)findViewById(R.id.top_bar_status_image_2)).setBackgroundColor(getResources().getColor(R.color.orange));
					((ImageView)findViewById(R.id.top_bar_status_image_1)).setBackgroundColor(getResources().getColor(R.color.default_white));
					updateInviteMessage1();
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		
		if(type ==1){
			updateInviteMessage();
		}else{
			updateInviteMessage1();
			viewPager.setCurrentItem(1);
		}
		
	}
	private class ViewAdapter extends PagerAdapter {
		@Override
		public void destroyItem(View arg0, int arg1, Object arg2) {
			((ViewPager) arg0).removeView(mSubViews.get(arg1));
		}

		@Override
		public void finishUpdate(View arg0) {
		}

		@Override
		public int getCount() {
			return mSubViews.size();
		}

		@Override
		public Object instantiateItem(View arg0, int position) {
			((ViewPager) arg0).addView(mSubViews.get(position), 0);
			return mSubViews.get(position);
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return arg0 == (arg1);
		}

		@Override
		public void restoreState(Parcelable arg0, ClassLoader arg1) {
		}

		@Override
		public Parcelable saveState() {
			return null;
		}

		@Override
		public void startUpdate(View arg0) {
		}
	}
	private void updateInviteMessage(){
		findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
		 RequestParams params = new RequestParams();
		 params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
		 HttpClient.getInstance().get(Constants.API_GET_CHART_MESSAGE, params, new MyResponseHandler(NewFriendsMsgActivity.this, GroupMessageResponse.class){
			 @Override
			 public void onSuccess(String result) {
					super.onSuccess(result);
					findViewById(R.id.progressBar).setVisibility(View.GONE);
					if(getDataSuccess){
						GroupMessageResponse res = (GroupMessageResponse)response;
						invites = res.getData();
						adapter.setData(invites);
						adapter.notifyDataSetChanged();
					}else{
						invites.clear();
						adapter.setData(invites);
						adapter.notifyDataSetChanged();
					}
			 }
			 
			 @Override
				public void onFailure(Throwable arg0, String result) {
				 super.onFailure(arg0);
				 findViewById(R.id.progressBar).setVisibility(View.GONE);
			 }
			 
		 });
	 }
	
	private void updateInviteMessage1(){
		findViewById(R.id.progressBar).setVisibility(View.VISIBLE);
		 RequestParams params = new RequestParams();
		 params.put("uId", BaseActivity.getMyInfo(getApplicationContext()).getuId());
		 HttpClient.getInstance().get(Constants.API_GET_CHART_INVITE, params, new MyResponseHandler(NewFriendsMsgActivity.this, GroupMessageResponse.class){
			 @Override
			 public void onSuccess(String result) {
					super.onSuccess(result);
					findViewById(R.id.progressBar).setVisibility(View.GONE);
					if(getDataSuccess){
						GroupMessageResponse res = (GroupMessageResponse)response;
						
						invites1 = res.getData();
						adapter1.setData(invites1);
						adapter1.notifyDataSetChanged();
						
					}else{
						invites1.clear();
						adapter1.setData(invites1);
						adapter1.notifyDataSetChanged();
					}
			 }
			 
			 @Override
				public void onFailure(Throwable arg0, String result) {
				 super.onFailure(arg0);
				 findViewById(R.id.progressBar).setVisibility(View.GONE);
			 }
			 
		 });
	 }
		 
	public void back(View view) {
		s_instance=null;
		ChatHistoryActivity.needRefresh = true;
		finish();
	}
	
	
}
